package com.avic.rong.questionOrAnswer.imgSelect;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.avic.rong.R;
import com.avic.rong.utils.GlideUtils;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.example.minato.minastore.utils.GlideApp;
import com.luck.picture.lib.entity.LocalMedia;


import java.util.ArrayList;
import java.util.List;


public class GridImageAdapter extends RecyclerView.Adapter<GridImageAdapter.ViewHolder> {

    public static final int TYPE_CAMERA = 1;

    public static final int TYPE_PICTURE = 2;


    private List<String> list = new ArrayList<>();

    private List<LocalMedia> mLocalList = new ArrayList<>();

    private int selectMax = 3;

    private Activity activity;

    /**
     * 点击添加图片跳转
     */
    private onAddPicClickListener mOnAddPicClickListener;

    public interface onAddPicClickListener {
        void onAddPicClick();
    }

    public GridImageAdapter(Activity activity, onAddPicClickListener mOnAddPicClickListener) {
        this.activity = (Activity) activity;
        this.mOnAddPicClickListener = mOnAddPicClickListener;
    }

    public void setSelectMax(int selectMax) {
        this.selectMax = selectMax;
    }

    public void setList(List<String> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView mImg;
        View iv_del;

        public ViewHolder(View view) {
            super(view);
            mImg = view.findViewById(R.id.fiv);
            iv_del = view.findViewById(R.id.iv_del);
        }
    }

    @Override
    public int getItemCount() {
        if (list.size() < selectMax) {
            return list.size() + 1;
        } else {
            return selectMax;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (isShowAddItem(position)) {
            return TYPE_CAMERA;
        } else {
            return TYPE_PICTURE;
        }
    }

    public List<LocalMedia> getmLocalList() {
        return mLocalList;
    }

    public void setmLocalList(List<LocalMedia> mLocalList) {
        this.mLocalList = mLocalList;
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = View.inflate(activity, R.layout.item_select_img, null);
        final ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    private boolean isShowAddItem(int position) {
        int size = list.size() == 0 ? 0 : list.size();
        return position == size;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
//        AppUtil.setImage(activity, viewHolder.mImg, width);
        //少于8张，显示继续添加的图标
        if (getItemViewType(position) == TYPE_CAMERA) {
//            viewHolder.mImg.setImageUrl(R.mipmap.upload_img);
//            GlideUtils.loaderImage(activity,R.mipmap.upload_img,viewHolder.mImg);
            RequestOptions options = new RequestOptions()
//                    .centerCrop()
                    .placeholder(R.mipmap.doc_default)
                    .dontAnimate()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .priority(Priority.NORMAL);
            GlideApp.with(activity).load(R.mipmap.upload_img).apply(options).into(viewHolder.mImg);
            viewHolder.mImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnAddPicClickListener.onAddPicClick();
                }
            });
            viewHolder.iv_del.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.iv_del.setVisibility(View.VISIBLE);
            viewHolder.iv_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int index = viewHolder.getAdapterPosition();
                    // 这里有时会返回-1造成数据下标越界,具体可参考getAdapterPosition()源码，
                    // 通过源码分析应该是bindViewHolder()暂未绘制完成导致，知道原因的也可联系我~感谢
                    if (index != RecyclerView.NO_POSITION) {
                        int i = list.size() - mLocalList.size();
                        if (index >= i) {
                            mLocalList.remove(index - i);
                            list.remove(index);
                            notifyItemRemoved(index);
                            notifyItemRangeChanged(index, list.size());
                        } else {
                            deletePhoto(index);
                        }
                    }
                }
            });
//            viewHolder.mImg.setImageUrl("file:///"+list.get(position));
            GlideUtils.loaderImage(activity,"file:///"+list.get(position),viewHolder.mImg);
//            RequestOptions options = new RequestOptions()
//                    .centerCrop()
//                    .placeholder(R.color.color_f6)
//                    .diskCacheStrategy(DiskCacheStrategy.ALL);
//
//            Glide.with(viewHolder.itemView.getContext())
//                    .load(list.get(position))
//                    .apply(options)
//                    .into(viewHolder.mImg);
        }
        //itemView 的点击事件
        if (mItemClickListener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int adapterPosition = viewHolder.getAdapterPosition();
                    mItemClickListener.onItemClick(adapterPosition, v);
                }
            });
        }
    }

    protected OnItemClickListener mItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(int position, View v);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    public void deletePhoto(final int index) {
        list.remove(index);
        notifyItemRemoved(index);
        notifyItemRangeChanged(index, list.size());
    }

}

